package com.gongj.rabbitboot.rabbitmq.cousumer;


import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gongj.rabbitboot.dto.PayDTO;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@Slf4j
public class RabbitReceiver {
//
//    @RabbitListener(queuesToDeclare = {@Queue(value = "hello-boot",durable = "false",autoDelete = "false")})
//    public void infoConsumption(@Payload String data, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
//        log.info("RabbitReceiver.infoConsumption() data = {}", data.toString());
//        ObjectMapper objectMapper = new ObjectMapper();
//        PayDTO payDTO = objectMapper.readValue(data, PayDTO.class);
//        log.info("getPayBank = {}====",payDTO.getPayBank());
//    }

    @RabbitListener(queuesToDeclare = {@Queue(value = "hello-boot",durable = "false",autoDelete = "false")})
    public void infoConsumption(@Payload JSONObject data, @Headers Map<String,Object> headers) throws IOException {
        log.info("RabbitReceiver.infoConsumption() data = {}", data.toJSONString());
        PayDTO pay = data.toJavaObject(PayDTO.class);
        for(Map.Entry<String, Object> map :headers.entrySet()){
            log.info("headers ===> " + map.getKey() + "=" + map.getValue());
        }
    }
}
